package com.struc.td;


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

//our highscore state class
public class Highscore extends State{	
	private Main m;
	private int width;
	private int height;
	private Texture highscore;
	private Texture highscoreBut;
	private Sprite bg;
	private Sprite backtoMenu;
	private Sprite highscoreButton;
	private SpriteBatch sb;
	private Preferences pref;
	private BitmapFont font;
	/* If it is the first time the application launches
	 * - initiate preferences.
	 */	
	public Highscore(Main m) {
		font = StaticTextures.font;
		pref = Gdx.app.getPreferences("my-preferences");
		if (!pref.contains("One")) {
			pref.putString("One","-");
			pref.putString("Two","-");
			pref.putString("Three","-");
			pref.putString("Four","-");
			pref.putString("Five","-");
		}
		this.m = m;
		highscore = new Texture(Gdx.files.internal("data/highscoreback.png"));
		highscoreBut = new Texture(Gdx.files.internal("data/highscoremenubut.png"));
		highscoreButton = new Sprite(highscoreBut,108,80,294,72);
		backtoMenu = new Sprite(highscoreBut, 120, 181, 171, 71);
		bg = new Sprite(highscore, 0, 0, 1024, 1024);
		sb = new SpriteBatch();
	}
	
	public void update() {
		width = Main.WIDTH;
		height = Main.HEIGHT;
		bg.setSize(width, height);
		highscoreButton.setBounds(Gdx.graphics.getWidth()/2-102, Gdx.graphics.getHeight()-50, 205, 50);
		backtoMenu.setBounds(Gdx.graphics.getWidth()/2-60, Gdx.graphics.getHeight()-220, 120, 50);
	}

	//renders highscore state and writes out highscores from our database
	public void render() {
		sb.begin();
		bg.draw(sb);
		highscoreButton.draw(sb);
		backtoMenu.draw(sb);
		font.draw(sb, pref.getString("One"), 100, 180);
		font.draw(sb, pref.getString("Two"), 100, 160);
		font.draw(sb, pref.getString("Three"), 100, 140);
		font.draw(sb, pref.getString("Four"), 100, 120);
		font.draw(sb, pref.getString("Five"), 100, 100);
		sb.end();		
	}

	public void enter(){
		Gdx.input.setInputProcessor(this);
	}

	public boolean keyDown(int arg0) {
		return false;
	}

	public boolean keyTyped(char arg0) {
		return false;
	}

	public boolean keyUp(int arg0) {
		return false;
	}

	public boolean scrolled(int arg0) {
		return false;
	}

	//only option on touch is to get back to main menu
	public boolean touchDown(int x, int y, int arg0, int arg1) {	
		y= Gdx.graphics.getHeight()-y;
		if (backtoMenu.getBoundingRectangle().contains(x,y)) {
			m.setState(1);
		}
		return false;
	}

	public boolean touchDragged(int arg0, int arg1, int arg2) {
		return false;
	}

	public boolean touchMoved(int arg0, int arg1) {
		return false;
	}

	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
		return false;
	}

}
